from django.db import models


# 基础数据


class stock_basic(models.Model):
    """
    股票基础表
    描述：获取基础信息数据，包括股票代码、名称、上市日期、退市日期等
    """
    ts_code = models.fields.TextField(max_length=10)
    symbol = models.fields.TextField(max_length=6, null=True, blank=True)
    name = models.fields.TextField(max_length=20)
    area = models.fields.TextField(max_length=20, null=True, blank=True)
    industry = models.fields.TextField(max_length=20, null=True, blank=True)
    fullname = models.fields.TextField(max_length=100, null=True, blank=True)
    enname = models.fields.TextField(max_length=100, null=True, blank=True)
    market = models.fields.TextField(max_length=10)
    exchange = models.fields.TextField(max_length=10, null=True, blank=True)
    curr_type = models.fields.TextField(max_length=10, null=True, blank=True)
    list_status = models.fields.TextField(max_length=10, null=True, blank=True)
    list_date = models.fields.TextField(max_length=10, null=True, blank=True)
    delist_date = models.fields.TextField(max_length=10, null=True, blank=True)
    is_hs = models.fields.TextField(max_length=10, null=True, blank=True)

    class Meta:
        db_table = 'stock_basic'  # 通过db_table自定义数据表名,而不会添加app前缀


class stock_company(models.Model):
    """
    上市公司基本信息
    接口：stock_company
    描述：获取上市公司基础信息
    积分：用户需要至少120积分才可以调取
    """
    ts_code = models.fields.TextField(max_length=10)
    exchange = models.fields.TextField(max_length=10, null=True, blank=True)
    chairman = models.fields.TextField(max_length=20, null=True, blank=True)
    manager = models.fields.TextField(max_length=20, null=True, blank=True)
    secretary = models.fields.TextField(max_length=20, null=True, blank=True)
    reg_capital = models.fields.TextField(max_length=20, null=True, blank=True)
    setup_date = models.fields.TextField(max_length=10, null=True, blank=True)
    province = models.fields.TextField(max_length=10, null=True, blank=True)
    city = models.fields.TextField(max_length=20, null=True, blank=True)
    introduction = models.fields.TextField(max_length=255, null=True, blank=True)
    website = models.fields.TextField(max_length=255, null=True, blank=True)
    email = models.fields.TextField(max_length=100, null=True, blank=True)
    office = models.fields.TextField(max_length=100, null=True, blank=True)
    employees = models.fields.IntegerField(null=True, blank=True, default=0)
    main_business = models.fields.TextField(max_length=100, null=True, blank=True)
    business_scope = models.fields.TextField(max_length=100, null=True, blank=True)

    class Meta:
        db_table = 'stock_company'  # 通过db_table自定义数据表名,而不会添加app前缀


# 行情数据


class daily(models.Model):
    """
    数据说明：交易日每天15点～16点之间。本接口是未复权行情，停牌期间不提供数据。
    调取说明：基础积分每分钟内最多调取200次，每次4000条数据，相当于超过18年历史，用户获得超过5000积分无频次限制。
    描述：获取股票行情数据，或通过通用行情接口获取数据，包含了前后复权数据。
    接口示例
    df = pro.daily(ts_code='000001.SZ', start_date='20180701', end_date='20180718')
    也可以通过日期取历史某一天的全部历史
    df = pro.daily(trade_date='20180810')
    """
    ts_code = models.fields.TextField(max_length=10)
    trade_date = models.fields.TextField(max_length=10)
    open = models.fields.FloatField(null=True, blank=True, default=0)
    high = models.fields.FloatField(null=True, blank=True, default=0)
    low = models.fields.FloatField(null=True, blank=True, default=0)
    close = models.fields.FloatField(null=True, blank=True, default=0)
    pre_close = models.fields.FloatField(null=True, blank=True, default=0)
    change = models.fields.FloatField(null=True, blank=True, default=0)
    pct_chg = models.fields.FloatField(null=True, blank=True, default=0)
    vol = models.fields.FloatField(null=True, blank=True, default=0)
    amount = models.fields.FloatField(null=True, blank=True, default=0)

    class Meta:
        db_table = 'daily'  # 通过db_table自定义数据表名,而不会添加app前缀


class daily_basic(models.Model):
    """
    接口：daily_basic
    更新时间：交易日每日15点～17点之间
    描述：获取全部股票每日重要的基本面指标，可用于选股分析、报表展示等。
    积分：用户需要至少300积分才可以调取，具体请参阅积分获取办法
    接口用法
    df = pro.daily_basic(ts_code='' or  trade_date='')
    """
    ts_code = models.fields.TextField(max_length=10)
    trade_date = models.fields.TextField(max_length=10)
    close = models.fields.FloatField(null=True, blank=True, default=0)
    turnover_rate = models.fields.FloatField(null=True, blank=True, default=0)
    turnover_rate_f = models.fields.FloatField(null=True, blank=True, default=0)
    volume_ratio = models.fields.FloatField(null=True, blank=True, default=0)
    pe = models.fields.FloatField(null=True, blank=True, default=0)
    pe_ttm = models.fields.FloatField(null=True, blank=True, default=0)
    pb = models.fields.FloatField(null=True, blank=True, default=0)
    ps = models.fields.FloatField(null=True, blank=True, default=0)
    ps_ttm = models.fields.FloatField(null=True, blank=True, default=0)
    #########20200109更新，tushare添加了股息率和股息率TTM两个指标
    dv_ratio = models.fields.FloatField(null=True, blank=True, default=0)
    dv_ttm = models.fields.FloatField(null=True, blank=True, default=0)
    ##########
    total_share = models.fields.FloatField(null=True, blank=True, default=0)
    float_share = models.fields.FloatField(null=True, blank=True, default=0)
    free_share = models.fields.FloatField(null=True, blank=True, default=0)
    total_mv = models.fields.FloatField(null=True, blank=True, default=0)
    circ_mv = models.fields.FloatField(null=True, blank=True, default=0)

    class Meta:
        db_table = 'daily_basic'  # 通过db_table自定义数据表名,而不会添加app前缀


class moneyflow(models.Model):
    """
    接口：moneyflow
    描述：获取沪深A股票资金流向数据，分析大单小单成交情况，用于判别资金动向
    限量：单次最大提取4000行记录，总量不限制
    积分：用户需要至少1500积分才可以调取
    接口示例#获取单日全部股票数据
    df = pro.moneyflow(trade_date='20190315')
    #获取单个股票数据
    df = pro.moneyflow(ts_code='002149.SZ', start_date='20190115', end_date='20190315')
    """
    ts_code = models.fields.TextField(max_length=10)
    trade_date = models.fields.TextField(max_length=10)
    buy_sm_vol = models.fields.IntegerField(null=True, blank=True, default=0)
    buy_sm_amount = models.fields.FloatField(null=True, blank=True, default=0)
    sell_sm_vol = models.fields.IntegerField(null=True, blank=True, default=0)
    sell_sm_amount = models.fields.FloatField(null=True, blank=True, default=0)
    buy_md_vol = models.fields.IntegerField(null=True, blank=True, default=0)
    buy_md_amount = models.fields.FloatField(null=True, blank=True, default=0)
    sell_md_vol = models.fields.IntegerField(null=True, blank=True, default=0)
    sell_md_amount = models.fields.FloatField(null=True, blank=True, default=0)
    buy_lg_vol = models.fields.IntegerField(null=True, blank=True, default=0)
    buy_lg_amount = models.fields.FloatField(null=True, blank=True, default=0)
    sell_lg_vol = models.fields.IntegerField(null=True, blank=True, default=0)
    sell_lg_amount = models.fields.FloatField(null=True, blank=True, default=0)
    buy_elg_vol = models.fields.IntegerField(null=True, blank=True, default=0)
    buy_elg_amount = models.fields.FloatField(null=True, blank=True, default=0)
    sell_elg_vol = models.fields.IntegerField(null=True, blank=True, default=0)
    sell_elg_amount = models.fields.FloatField(null=True, blank=True, default=0)
    net_mf_vol = models.fields.IntegerField(null=True, blank=True, default=0)
    net_mf_amount = models.fields.FloatField(null=True, blank=True, default=0)

    class Meta:
        db_table = 'moneyflow'  # 通过db_table自定义数据表名,而不会添加app前缀


class limit_list(models.Model):
    """
    每日涨跌停统计
    接口：limit_list
    描述：获取每日涨跌停股票统计，包括封闭时间和打开次数等数据，帮助用户快速定位近期强（弱）势股，以及研究超短线策略。
    限量：单次最大1000，总量不限制
    接口示例
    #获取单日统计数据
    df = pro.limit_list(trade_date='20190925')
    #获取某日涨停股票，并指定字段输出
    df = pro.limit_list(trade_date='20190925', limit_type='U', fields='ts_code,close,first_time,last_time')
    #获取时间段统计信息
    df = pro.limit_list(start_date='20190920', end_date='20190925')
    """
    trade_date = models.fields.TextField(max_length=10)
    ts_code = models.fields.TextField(max_length=10)
    name = models.fields.TextField(max_length=20)
    close = models.fields.FloatField(null=True, blank=True, default=0)
    pct_chg = models.fields.FloatField(null=True, blank=True, default=0)
    amp = models.fields.FloatField(null=True, blank=True, default=0)
    fc_ratio = models.fields.FloatField(null=True, blank=True, default=0)
    fl_ratio = models.fields.FloatField(null=True, blank=True, default=0)
    fd_amount = models.fields.FloatField(null=True, blank=True, default=0)
    first_time = models.fields.TextField(max_length=10, null=True, blank=True)
    last_time = models.fields.TextField(max_length=10, null=True, blank=True)
    open_times = models.fields.IntegerField(null=True, blank=True, default=0)
    strth = models.fields.FloatField(null=True, blank=True, default=0)
    limit = models.fields.TextField(max_length=10, null=True, blank=True)

    class Meta:
        db_table = 'limit_list'  # 通过db_table自定义数据表名,而不会添加app前缀


# 市场参考数据
class block_trade(models.Model):
    """
    接口：block_trade
    描述：大宗交易
    限量：单次最大1000条，总量不限制
    积分：300积分可调取，每分钟内限制次数，超过5000积分无限制，
    接口使用
    df = pro.block_trade(trade_date='20181227')
    """
    ts_code = models.fields.TextField(max_length=10)
    trade_date = models.fields.TextField(max_length=10)
    price = models.fields.FloatField(null=True, blank=True, default=0)
    vol = models.fields.FloatField(null=True, blank=True, default=0)
    amount = models.fields.FloatField(null=True, blank=True, default=0)
    buyer = models.fields.TextField(max_length=100, null=True, blank=True)
    seller = models.fields.TextField(max_length=100, null=True, blank=True)

    class Meta:
        db_table = 'block_trade'  # 通过db_table自定义数据表名,而不会添加app前缀


class stk_holdernumber(models.Model):
    """
    接口：stk_holdernumber
    描述：获取上市公司股东户数数据，数据不定期公布
    限量：单次最大3000,总量不限制
    积分：600积分可调取，基础积分每分钟调取100次，5000积分以上无限制。
    接口使用
    df = pro.stk_holdernumber(ts_code='300199.SZ')
    """
    ts_code = models.fields.TextField(max_length=10)
    ann_date = models.fields.TextField(max_length=10, null=True, blank=True)
    end_date = models.fields.TextField(max_length=20, null=True, blank=True)
    holder_num = models.fields.IntegerField(null=True, blank=True, default=0)

    class Meta:
        db_table = 'stk_holdernumber'  # 通过db_table自定义数据表名,而不会添加app前缀


class concept(models.Model):
    """
    概念股分类
    接口：concept
    描述：获取概念股分类，目前只有ts一个来源，未来将逐步增加来源
    接口使用
    df = pro.concept()
    或者
    df = pro.concept(src='ts')
    """
    code = models.fields.TextField(max_length=10)
    name = models.fields.TextField(max_length=20, )
    src = models.fields.TextField(max_length=10, null=True, blank=True)

    class Meta:
        db_table = 'concept'  # 通过db_table自定义数据表名,而不会添加app前缀


class concept_detail(models.Model):
    """
    概念股列表
    接口：concept_detail
    描述：获取概念股分类明细数据,该表需和概念列表配合使用
    接口使用
    #取5G概念明细
    df = pro.concept_detail(id='TS2', fields='ts_code,name')
    #或者查询某个股票的概念
    df = pro.concept_detail(ts_code = '600848.SH')
    """
    code = models.fields.TextField(max_length=10, null=True,
                                   blank=True)  # 原字段使id，由于和django自加字段id冲突，所以改为code，在插入数据时需要注意
    concept_name = models.fields.TextField(max_length=20, null=True, blank=True)
    ts_code = models.fields.TextField(max_length=10, null=True, blank=True)
    name = models.fields.TextField(max_length=10, null=True, blank=True)
    in_date = models.fields.TextField(max_length=20, null=True, blank=True)
    out_date = models.fields.TextField(max_length=10, null=True, blank=True)

    class Meta:
        db_table = 'concept_detail'  # 通过db_table自定义数据表名,而不会添加app前缀


# 数据库基础数据表，记录各个表字段和对应中文
class table_info(models.Model):
    tablename = models.fields.TextField(max_length=100)
    tableremark = models.fields.TextField(max_length=100, null=True, blank=True)
    column = models.fields.TextField(max_length=100)
    type = models.fields.TextField(max_length=100, null=True, blank=True)
    remark = models.fields.TextField(max_length=100, null=True, blank=True)

    class Meta:
        db_table = 'table_info'  # 通过db_table自定义数据表名,而不会添加app前缀


class pre_analysis_moneyflow(models.Model):
    """
    提前分析用的数据临时表，提前将连续两天资金净流入或流出的股票存入数据库，便于后期快速查找，提高查找效率
    """
    ts_code = models.fields.TextField(max_length=10)
    name = models.fields.TextField(max_length=10, blank=True, null=True)  # 股票名称
    #analysis_date = models.fields.TextField (max_length=10, blank=True, null=True)  # 分析日期，按交易表中最新日期记录
    in_or_out = models.fields.TextField(max_length=4)  # 值为in或者out
    continues_days = models.fields.IntegerField(default=2, blank=True, null=True)

    class Meta:
        db_table = 'pre_analysis_moneyflow'  # 通过db_table自定义数据表名,而不会添加app前缀


class pre_analysis_updown(models.Model):
    """
    提前分析用的数据临时表，提前将连续两天上涨或下跌的股票存入数据库，便于后期快速查找，提高查找效率
    """
    ts_code = models.fields.TextField(max_length=10)
    name = models.fields.TextField(max_length=10, blank=True, null=True)  # 股票名称
    #analysis_date = models.fields.TextField(max_length=10, blank=True, null=True)  # 分析日期，按交易表中最新日期记录
    up_or_down = models.fields.TextField(max_length=4)  # 值为up或者dwon
    continues_days = models.fields.IntegerField(default=2, blank=True, null=True)

    class Meta:
        db_table = 'pre_analysis_updown'  # 通过db_table自定义数据表名,而不会添加app前缀


class pre_analysis_pct_chg(models.Model):
    """
    提前分析用的数据临时表，提前将连续三天上涨或下跌幅度超过10%的股票存入数据库，便于后期快速查找，提高查找效率
    """
    ts_code = models.fields.TextField(max_length=10)
    name = models.fields.TextField(max_length=10, blank=True, null=True)  # 股票名称
    #analysis_date = models.fields.TextField(max_length=10, blank=True, null=True)  # 分析日期，按交易表中最新日期记录
    up_or_down = models.fields.TextField(max_length=4)  # 值为up或者down
    continues_days = models.fields.IntegerField(default=2, blank=True, null=True)
    pct_chg = models.fields.FloatField(null=True, blank=True, default=0)

    class Meta:
        db_table = 'pre_analysis_pct_chg'  # 通过db_table自定义数据表名,而不会添加app前缀


class pre_analysis_main_moneyflow(models.Model):
    """
    提前分析主力净流入资金单位万元，主力占换手率比例%，
    """
    ts_code = models.fields.TextField(max_length=10)
    trade_date = models.fields.TextField(max_length=10)
    main_in = models.fields.FloatField(null=True, blank=True, default=0)
    main_turnover_rate = models.fields.FloatField(null=True, blank=True, default=0)

    class Meta:
        db_table = 'pre_analysis_main_moneyflow'  # 通过db_table自定义数据表名,而不会添加app前缀

class attention_stock(models.Model):
    """
    关注股票信息
    """
    ts_code = models.fields.TextField(max_length=10)
    name = models.fields.TextField(max_length=20,null=True, blank=True)
    start_attention_date = models.fields.TextField(max_length=10) #开始关注日期
    start_attention_price  =  models.fields.FloatField(null=True, blank=True, default=0)#开始关注收盘价
    end_attention_data = models.fields.TextField(max_length=10,null=True, blank=True,)#结束关注日期
    end_attention_price =models.fields.FloatField(null=True, blank=True, default=0)#结束关注收盘价
    is_end = models.fields.BooleanField(null=True, blank=True, default=1)#判断是否结束关注，默认1关注 0结束关注
    strategy = models.fields.TextField(null=True, blank=True)

    class Meta:
        db_table = 'attention_stock'  # 通过db_table自定义数据表名,而不会添加app前缀


